package com.linck.im.client.handler;

import com.linck.im.client.cache.AccountCache;
import com.linck.im.model.proto.ProtoMsg;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * 系统消息处理器
 *
 * @author: linck
 * @create: 2021-01-25
 */
@Slf4j
public class ClientSysMsgHandler extends SimpleChannelInboundHandler<ProtoMsg.SysMsg> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ProtoMsg.SysMsg sysMsg) throws Exception {
        log.info("系统信息：code:{} msg:{}", sysMsg.getCode(), sysMsg.getMsg());
        // 登录事件，登录计数器+1
        if (sysMsg.getCode().equals(ProtoMsg.MsgCode.LOGIN_SUCCESS)) {
            AccountCache.isLogin = true;
            AccountCache.count.incrementAndGet();
        } else if (sysMsg.getCode().equals(ProtoMsg.MsgCode.LOGIN_FAILURE)) {
            AccountCache.count.incrementAndGet();
        }
    }

    // 连接断开，设置未未登录
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        AccountCache.isLogin = false;
    }
}
